package controller;

import java.util.Iterator;

import main.model.Diagram;
import main.model.Entity;
import view.AddEntityToHierarchyView;

public class AddEntityToHierarchyController extends Controller{
	
	private Diagram diagram;
	private AddEntityToHierarchyView view;
	private CreateHierarchyController controller;
	private Boolean isParentAdd;
	
	public AddEntityToHierarchyController(Diagram diagram,CreateHierarchyController controller, Boolean isParent){
		this.diagram = diagram;
		this.controller = controller;
		this.isParentAdd = isParent;
	}

	@Override
	public void displayView() {
		String[] entities = new String[diagram.getEntitiesNames().size()];
		Integer i = 0;
		for (String string : diagram.getEntitiesNames()) {
			entities[i] = string;
			i++;
		}
		view = new AddEntityToHierarchyView(entities);
		view.setController(this);
		view.show();
	}

	public void handleButtonTerminar(){
		String selectedEntity = view.getEntitiesList();
		Entity e = null;
		Iterator<Entity> iterator = diagram.getEntities().iterator();
		Boolean found = false;
		while (!found && iterator.hasNext()){
			Entity next = iterator.next();
			if (next.getName().equals(selectedEntity)){
				found = Boolean.TRUE;
				e = next;
			}
		}
		if (isParentAdd){
			controller.addParentToHierarchy(e);
		} else {
			controller.addEntityToHierarchy(e);
		}
		view.hide();
	}
	
}
